package com.ruoyi.recycleshop.controller;

import cn.hutool.core.io.IoUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.recycle.domain.Coupon;
import com.ruoyi.recycle.dto.NativePayDTO;
import com.ruoyi.recycle.service.CouponService;
import com.ruoyi.recycle.vo.CouponInfoVO;
import com.ruoyi.recycleshop.domain.ShopExchangeRecord;
import com.ruoyi.recycleshop.dto.ShopExchangeQueryDTO;
import com.ruoyi.recycleshop.service.CashRegisterService;
import com.ruoyi.recycleshop.utils.QRCodeUtil;
import com.ruoyi.recycleshop.vo.ExchangeStatisticsVO;
import com.ruoyi.recycleshop.vo.PayInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cashRegister")
@Api(tags = "收银")
@Slf4j
public class CashRegisterController extends BaseController {
    @Autowired
    private CashRegisterService cashRegisterService;
    @Autowired
    private CouponService couponService;

    @PostMapping("/exchangeList")
    @ApiOperation("交易记录")
    public R<List<ShopExchangeRecord>> exchangeList(@RequestBody ShopExchangeQueryDTO queryDTO) {
        return R.ok(cashRegisterService.exchangeList(queryDTO));
    }

    @GetMapping("/listCoupons")
    @ApiOperation("引流平台优惠券列表")
    @ApiImplicitParam(name = "shopId", value = "引流平台Id")
    public R<List<Coupon>> listCoupons(@RequestParam Long shopId) {
        final LambdaQueryWrapper<Coupon> wrapper = Wrappers.lambdaQuery(new Coupon()).eq(Coupon::getShopId, shopId);
        final List<Coupon> couponList = couponService.list(wrapper);
        return R.ok(couponList);
    }

    @PostMapping("/createCoupon")
    @ApiOperation("创建优惠券")
    @SneakyThrows
    public R<Boolean> createCoupon(@RequestBody Coupon coupon) {
        return R.ok(cashRegisterService.createCoupon(coupon));
    }

    @PostMapping("/modifyCoupon")
    @ApiOperation("修改优惠券")
    public R<Boolean> modifyCoupon(@RequestBody Coupon coupon) {
        return R.ok(cashRegisterService.modifyCoupon(coupon));
    }

    @GetMapping("/getCouponInfo")
    @ApiOperation("获取优惠券信息")
    @ApiImplicitParam(name = "couponId", value = "优惠券ID")
    public R<Coupon> getCouponInfo(@RequestParam Long couponId) {
        return R.ok(couponService.getById(couponId));
    }

    @GetMapping("/getCouponDetail")
    @ApiOperation("二维码扫描后获取优惠券具体信息")
    @ApiImplicitParam(name = "couponNo", value = "优惠券号")
    public R<CouponInfoVO> getCouponDetail(@RequestParam String couponNo) {
        return R.ok(cashRegisterService.getCouponDetail(couponNo));
    }

    @GetMapping("/deleteCoupon")
    @ApiOperation("删除优惠券")
    @ApiImplicitParam(name = "couponId", value = "优惠券ID")
    public R<Boolean> deleteCoupon(@RequestParam Long couponId) {
        return R.ok(cashRegisterService.deleteCoupon(couponId));
    }

    @PostMapping("/blueCoinPay")
    @ApiOperation("纯蓝星币支付")
    public R<Boolean> blueCoinPay(@RequestBody NativePayDTO payDTO) {
        return R.ok(cashRegisterService.blueCoinPay(payDTO));
    }

    @PostMapping("/pay")
    @ApiOperation("混合支付或微信支付")
    @SneakyThrows
    public void pay(@RequestBody NativePayDTO payDTO, HttpServletResponse response) {
        final String codeUrl = cashRegisterService.pay(payDTO);
        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            QRCodeUtil.encode(codeUrl, outputStream);
        } catch (Exception e) {
            log.info("生成支付二维码错误,原因:{}", e.getMessage());
        } finally {
            IoUtil.close(outputStream);
        }
    }

    @PostMapping("/payCallBack")
    public Map<String, String> payCallBack(HttpServletRequest request) {
        return cashRegisterService.payCallBack(request);
    }


    @GetMapping("/exchangeStatistics")
    @ApiOperation("交易统计")
    @ApiImplicitParam(name = "shopId", value = "门店ID")
    public R<ExchangeStatisticsVO> exchangeStatistics(@RequestParam Long shopId) {
        return R.ok(cashRegisterService.exchangeStatistics(shopId));
    }

    @GetMapping("/payCompute")
    @ApiOperation("计算实际支付金额")
    @ApiImplicitParams({@ApiImplicitParam(name = "couponNo", value = "优惠券号"), @ApiImplicitParam(name = "amount", value = "支付金额")})
    public R<PayInfoVO> payCompute(@RequestParam String couponNo, @RequestParam BigDecimal amount) {
        return R.ok(cashRegisterService.payCompute(couponNo, amount));
    }


}
